import store from '@/store/index.js';
var TreeMoveNodeCommand = function() {
	
};

TreeMoveNodeCommand.prototype.execute = function() {
	var componentTreeModel=store.state.formDesigner.$refs.componentTreeDialog.componentTreeModel[0]
	var tempComponentModel=store.state.formDesigner.componentModel;
	var modelArray=[];
	this.getComponentModelArray(tempComponentModel,modelArray);
	
	for(var i=0;i<modelArray.length;i++){
		modelArray[i]["children"]=[];
		delete modelArray[i]["selectedChildModel"];
	}
	this.reSetComponentModel(componentTreeModel,tempComponentModel,modelArray);
	
	store.state.formDesigner.setComponentModel(tempComponentModel);
};

TreeMoveNodeCommand.prototype.reSetComponentModel=function(componentTreeModel,tempComponentModel,modelArray){
	if(tempComponentModel==undefined ||componentTreeModel==undefined){
		return;
	}
	var children=componentTreeModel.children;
	if(children!=undefined){
		for(var i=0;i<children.length;i++){
			var tempChildren=tempComponentModel.children;
			if(tempChildren==undefined){
				tempChildren=[];
				tempComponentModel.children=tempChildren;
			}
			var id=children[i].id;
			for(var j=0;j<modelArray.length;j++){
				if(modelArray[j].id==id){
					tempChildren.push(modelArray[j]);
				}
			}
			this.reSetComponentModel(children[i],tempChildren[i],modelArray);
		}
	}
};


TreeMoveNodeCommand.prototype.getComponentModelArray=function(componentModel, modelArray){
	modelArray.push(componentModel);
	var children = componentModel.children;
	if (children != undefined && children != null) {
		for (var i = 0; i < children.length; i++) {
			this.getComponentModelArray(children[i], modelArray);
		}
	}
}
export default TreeMoveNodeCommand
